package com.SNQ.project.English_speaking_learning_system.controller.user;

import com.SNQ.project.English_speaking_learning_system.common.result.Result;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.UserDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.UserLoginDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.entity.User;
import com.SNQ.project.English_speaking_learning_system.pojo.vo.UserLoginVO;
import com.SNQ.project.English_speaking_learning_system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/user")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}",userLoginDTO);

        User user = userService.login(userLoginDTO);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .build();
        return Result.success(userLoginVO);
    }

    @PostMapping
    @ApiOperation("用户注册")
    public Result save(@RequestBody UserDTO userDTO){
        log.info("新增用户：{}",userDTO);
        userService.enroll(userDTO);
        return Result.success();
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }

}
